<!DOCTYPE HTML>
<html>

<head>
    <title>proton.js-behaviour-custom</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            background-color: #fff;
            margin: 0px;
            overflow: hidden;
        }
        
        #collie_container {
            width: 1003px;
            margin-top: 50px;
            margin-left: -501px;
            left: 50%;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="container">

    </div>
    <script src="//localhost:3001/build/proton.js"></script>
    <script src="js/collie.min.js"></script>
    <script>
        var bird, rabbit, blood;
        var proton;
        var renderer;
        var emitter;
        var stats;
        var layer;
        var begin = true;

        Main();

        function Main() {
            setupCollieEngine();
            createProton();
        }

        function setupCollieEngine() {
            collie.ImageManager.add({
                rabbit: "image/yame_walk2.png",
                bird: "image/bird.png",
                sky: "image/sky.png",
                ground: "image/ground2.png",
                blood: "image/blood.png"
            });

            layer = new collie.Layer({
                width: 320,
                height: 480
            });

            var sky = new collie.DisplayObject({
                width: 320,
                height: 480,
                backgroundImage: "sky",
                backgroundRepeat: "repeat"
            }).addTo(layer);

            blood = new collie.DisplayObject({
                width: 20,
                height: 29,
                backgroundImage: "blood",
            });

            rabbit = new collie.DisplayObject({
                x: "center",
                width: 129.4,
                height: 165,
                zIndex: 2,
                backgroundImage: "rabbit"
            }).bottom(50).addTo(layer);

            bird = new collie.DisplayObject({
                width: 50,
                height: 50,
                x: 'center',
                y: 190,
                visible: true,
                backgroundImage: "bird"
            }).addTo(layer);

            collie.Timer.cycle(bird, 100, {
                to: 1
            });

            var currentY = rabbit.get("y");

            rabbit.attach({
                click: function(e) {
                    collie.Timer.queue().transition(rabbit, 400, {
                        to: currentY - 100,
                        effect: collie.Effect.easeOut,
                        set: "y"
                    }).transition(rabbit, 400, {
                        to: currentY,
                        effect: collie.Effect.easeIn,
                        set: "y"
                    });
                }
            });

            collie.Timer.cycle(rabbit, "18fps", {
                from: 0,
                to: 8,
                loop: 0
            });

            var ground = new collie.MovableObject({
                x: 0,
                width: 320 * 2,
                height: 88,
                velocityX: -200,
                backgroundImage: "ground",
                backgroundRepeat: "repeat-x",
                rangeX: [-320, 0],
                positionRepeat: true
            }).bottom(0).addTo(layer);

            collie.Renderer.addLayer(layer);
            collie.Renderer.load(document.getElementById("container"));
            collie.Renderer.start();

            var birdx = bird.get("x");
            collie.Timer.repeat(function() {
                proton.update();
                if (rabbit.get('y') - 15 <= bird.get('y')) {
                    if (begin) {
                        collie.Timer.transition(bird, 500, {
                            to: [0, 0, 400, 100, 80],
                            set: ["scaleX", "scaleY", "angle", "y", 'x']
                        });

                        emitter.p.x = bird.get("x") + 10;
                        emitter.p.y = bird.get("y") + 10;
                        emitter.emit('once');
                        setTimeout(function() {
                            bird.set('x', birdx);
                            bird.set('y', 190);
                            collie.Timer.transition(bird, 300, {
                                to: [1, 1, 0],
                                set: ["scaleX", "scaleY", "angle"],
                                effect: collie.Effect.overphase
                            });
                            begin = true;
                        }, 1600);
                        begin = false;
                    }
                }
            }, 10);
        }

        function createProton() {
            proton = new Proton;
            emitter = new Proton.Emitter();
            emitter.rate = new Proton.Rate(new Proton.Span(16, 18), new Proton.Span(.05, .2));

            emitter.addInitialize(new Proton.Mass(1));
            emitter.addInitialize(new Proton.Body(blood));
            emitter.addInitialize(new Proton.Life(1));

            emitter.addInitialize(new Proton.Velocity(new Proton.Span(1, 2), new Proton.Span(0, 80, true), 'polar'));
            emitter.addBehaviour(new Proton.Alpha(1, 0));
            emitter.addBehaviour(new Proton.Gravity(3));
            emitter.addBehaviour(new Proton.Rotate());
            emitter.addBehaviour(new Proton.Scale(0, Proton.getSpan(0.3, 0.5), Infinity, Proton.easeOutCubic));

            proton.addEmitter(emitter);
            proton.addRenderer(createRenderer());
        }

        function createRenderer() {
            var renderer = new Proton.CustomRenderer();

            renderer.onParticleCreated = function(particle) {
                var p = particle.body.clone().addTo(layer);
                particle.sprite = p;
            };

            renderer.onParticleUpdate = function(particle) {
                if (!!particle['sprite']) {
                    var mc = particle['sprite'];
                    mc.set('x', particle.p.x);
                    mc.set('y', particle.p.y);
                    mc.set('angle', particle.rotation);
                    mc.set('scaleX', particle.scale);
                    mc.set('scaleY', particle.scale);
                    mc.set('opacity', particle.alpha);
                }
            };

            renderer.onParticleDead = function(particle) {
                layer.removeChild(particle.sprite);
            };

            return renderer;
        }
    </script>
</body>

</html>